﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using lucienneCRMApp.Models;
using System.Net;
using System.Web.Helpers;

namespace lucienneCRMApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "CRM Application";
            return View();
        }

        public ActionResult ContactUs()
        {
            return View(new ContactUs());
        }

        public ActionResult Survey()
        {
            return View(new Survey());
        }
               
        [HttpPost]
        public ActionResult ContactUs(ContactUs model)
        {
            string retValue = "There was an error submitting the form, please try again later.";
            if (!ModelState.IsValid)
            {
                return Content(retValue);
            }
            if (ModelState.IsValid)
            {

                WebMail.SmtpServer = "smtp.gmail.com";
                WebMail.EnableSsl = true;
                WebMail.SmtpPort = 587;
                WebMail.UserName = "divecrm@gmail.com";
                WebMail.Password = "divecrm123";
                
                try
                    {            
                        WebMail.Send("divecrm@gmail.com", "Sender email: " + model.CEmail, model.Message);
                        retValue = "Your Request for Contact was submitted successfully. We will contact you shortly.";
                        return RedirectToAction("Index");
                    }
                    catch (Exception)
                    {
         
                        throw;
                    }
                
            }
            return Content(retValue);
        }


        

        [HttpPost]
        public ActionResult Survey(Survey model)
        {
            string retValue = "There was an error submitting the form, please try again later.";
            if (!ModelState.IsValid)
            {
                return Content(retValue);
            }
            if (ModelState.IsValid)
            {

                WebMail.SmtpServer = "smtp.gmail.com";
                WebMail.EnableSsl = true;
                WebMail.SmtpPort = 587;
                WebMail.UserName = "divecrm@gmail.com";
                WebMail.Password = "divecrm123";

                try
                {
                    string Fbody = "Question 1 : " + model.question1 + "\n" +  "Question 2: " + model.question2 +
                                     "\n" + "Question 3: " +  model.question3 + "\n" + "Question 4: " + model.question4;
                    WebMail.Send("divecrm@gmail.com", "Sender email: " + model.CEmail, Fbody);
                    retValue = "Your Request for Contact was submitted successfully. We will contact you shortly.";
                    return RedirectToAction("Index");
                }
                catch (Exception)
                {
                    //show confirmation

                    throw;
                }

            }
            return Content(retValue);
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Statistics()
        {
            return View();
        }
        
    }
}
